strana 258
----------
<Grid x:Name="LayoutRoot" Background="White">
  <StackPanel Orientation="Vertical">
    <Button Content="Vyber soubor" Click="Button_Click" />
    <TextBlock x:Name="tbOznam" FontSize="16" />
  </StackPanel>
</Grid>


C#:
private void Button_Click(object sender, RoutedEventArgs e)
{
  OpenFileDialog dlg = new OpenFileDialog();
  dlg.Multiselect = false;
  dlg.Filter = "All files|*.*";

  if ((bool)dlg.ShowDialog())
  {
    tbOznam.Text = dlg.File.Name;
    PrenosSouboru(dlg.File.Name, dlg.File.OpenRead());
  }
  else
  {
    tbOznam.Text = "Nebyl vybrn soubor !!!";
  }
}


C#:
private void PrenosSouboru(string nazev, Stream stream)
{
  UriBuilder ub = new UriBuilder("http://localhost");
  ub.Query = string.Format("filename={0}", nazev);

  WebClient c = new WebClient();
  c.OpenWriteCompleted += (sender, e) =>
  {
    Prenos(stream, e.Result);
    e.Result.Close();
    stream.Close();
  };
  c.OpenWriteAsync(ub.Uri);
}

private void Prenos(Stream inp, Stream outp)
{
  byte[] buffer = new byte[4096];
  int bytesRead;

  while ((bytesRead = inp.Read(buffer, 0, buffer.Length)) != 0)
  {
    outp.Write(buffer, 0, bytesRead);
  }
}



strana 260
----------
C#:
public void ProcessRequest(HttpContext context)
{
  string nazev = context.Request.QueryString["filename"].ToString();
  using (FileStream fs = File.Create(context.Server.MapPath("~/Data/" + nazev)))
  {
    SaveFile(context.Request.InputStream, fs);
  }
}


private void SaveFile(Stream stream, FileStream fs)
{
  byte[] buffer = new byte[4096];
  int bytesRead;
  while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
  {
    fs.Write(buffer, 0, bytesRead);
  }
}




strana 262
----------
C#:
private void PrenosSouboru(string nazev, Stream stream)
{
  UriBuilder ub = new UriBuilder("http://localhost");
  ub.Query = string.Format("filename={0}", nazev);

  WebClient c = new WebClient();
  c.OpenWriteCompleted += (sender, e) =>
  {
    Prenos(stream, e.Result);
    e.Result.Close();
    stream.Close();
  };
  c.OpenWriteAsync(ub.Uri);
}



strana 263
----------
<Grid x:Name="LayoutRoot" Background="White">
  <TextBox x:Name="tbText" Height="200" TextWrapping="Wrap"
    Text="Vstup textu..."></TextBox>
  <Button x:Name="btUloz" Height="30" VerticalAlignment="Bottom" 
    Content="Ulo text" Click="btUloz_Click" />
</Grid>


C#:

private void btUloz_Click(object sender, System.Windows.RoutedEventArgs e)
{
  SaveFileDialog sfdUloz = new SaveFileDialog();
  bool? sf = sfdUloz.ShowDialog();
  if (sf == true)
  {
    using (Stream fs = (Stream)sfdUloz.OpenFile())
    {
      byte[] info = (new UTF8Encoding(true)).GetBytes(tbText.Text);
      fs.Write(info, 0, info.Length);
      fs.Close();
    }
  } 
}





strana 264
----------
C#:
using System.IO;
using System.Text;
using System.Net;

namespace SL3out
{
  public partial class MainControl : UserControl
  {
    SaveFileDialog sfd = new SaveFileDialog();
    public MainControl()
    {
      // Required to initialize variables
      InitializeComponent();
    }

    private void btUloz_Click(object sender, 
      System.Windows.RoutedEventArgs e)
    {
      sfd.DefaultExt = ".jpg";
      sfd.Filter = "JPG File|*.jpg|All Files|*.*";
      bool? open = sfd.ShowDialog();

      if (open.HasValue && open.Value)
      {
        Uri ur = new Uri(tbURL.Text);
        WebClient wc = new WebClient();
        wc.OpenReadAsync(ur);
        wc.OpenReadCompleted += 
          new OpenReadCompletedEventHandler(Ukladani);
      }
    }

    void Ukladani(object sender, OpenReadCompletedEventArgs e)
    {
      if (!e.Cancelled)
      {
        using (Stream fs = sfd.OpenFile())
        {
          int length = Convert.ToInt32(e.Result.Length);
          byte[] byteResult = new byte[length];
          e.Result.Read(byteResult, 0, length);
          fs.Write(byteResult, 0, byteResult.Length);
          fs.Close();
        }
      }
    }
  }
}



strana 266
----------
<Grid x:Name="LayoutRoot" Background="White">
  <StackPanel VerticalAlignment="Top" >
    <StackPanel Height="30" Orientation="Horizontal" Margin="10">
      <TextBlock Text="Nzev: " VerticalAlignment="Center"/>
      <TextBox x:Name="tbNazev" Text="Jmno" Width="150" Margin="0,0,5,0"/>
      <TextBlock Text="Hodnota: " VerticalAlignment="Center"/>
      <TextBox x:Name="tbHodnota" Width="150" Margin="0,0,5,0"/>
    </StackPanel>
    <StackPanel Height="30" Orientation="Horizontal" Margin="10">
      <Button x:Name="btnAdd" Content="Zpis" Click="btnAdd_Click" />
      <Button x:Name="btnChange" Content="Zmna" Click="btnChange_Click" />
      <Button x:Name="btnRemove" Content="Vyma" Click="btnRemove_Click" />
    </StackPanel>
    <StackPanel Height="30" Orientation="Horizontal" Margin="10">
      <Button x:Name="btnClear" Content="Vyma" Click="btnClear_Click" />
      <Button x:Name="btnCount" Content="Poet" Click="btnCount_Click" />
      <Button x:Name="btnKeys" Content="Nzvy" Click="btnKeys_Click" />
      <Button x:Name="btnValues" Content="Hodnoty" Click="btnValues_Click" />
    </StackPanel>
    <TextBox x:Name="tbOznam" Width="380"/>
  </StackPanel>
</Grid>



strana 267
----------
C#:
private IsolatedStorageSettings isData = 
  IsolatedStorageSettings.ApplicationSettings;

public MainPage()
{
  InitializeComponent();
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
  try
  {
    isData.Add(tbNazev.Text, tbHodnota.Text);
  }
  catch (ArgumentException ex)
  {
    tbOznam.Text = ex.Message;
  }
}

private void btnChange_Click(object sender, RoutedEventArgs e)
{
  isData[tbNazev.Text] = tbHodnota.Text;
  tbOznam.Text = "Obnovte strnku pro aktualizaci.";
}

private void btnRemove_Click(object sender, RoutedEventArgs e)
{
  if (isData.Remove(tbNazev.Text) == true)
  {
    tbOznam.Text = "REMOVE>> Obnovte strnku pro aktualizaci.";
  }
  else
  {
    tbOznam.Text = "Nzev neexistuje.";
  }
}

private void btnClear_Click(object sender, RoutedEventArgs e)
{
  isData.Clear();
  tbOznam.Text = "CLEAR>> Obnovte strnku pro aktualizaci ";
}

private void btnCount_Click(object sender, RoutedEventArgs e)
{
  tbOznam.Text = "Poet: " + isData.Count();
}

private void btnKeys_Click(object sender, RoutedEventArgs e)
{
  System.Text.StringBuilder sb = new System.Text.StringBuilder("Nzvy: ");
  foreach (string k in isData.Keys)
  {
    sb.Append(k + "; ");
  }
  tbOznam.Text = sb.ToString();
}

private void btnValues_Click(object sender, RoutedEventArgs e)
{
  System.Text.StringBuilder sb = new System.Text.StringBuilder("Hodnoty: ");
  foreach (Object v in isData.Values)
  {
    sb.Append(v.ToString() + "; ");
  }
  tbOznam.Text = sb.ToString();
}





strana 269
----------
<Grid x:Name="LayoutRoot" Background="White">
  <StackPanel VerticalAlignment="Top" >
    <StackPanel Height="30" Orientation="Horizontal" Margin="10">
      <TextBlock Text="Nzev: " VerticalAlignment="Center"/>
      <TextBox x:Name="tbNazev" Text="Jmeno" Width="150" Margin="0,0,5,0"/>
      <TextBlock Text="Hodnota:  " VerticalAlignment="Center"/>
      <TextBox x:Name="tbHodnota" Width="150" Margin="0,0,5,0"/>
    </StackPanel>
    <StackPanel Height="30" Orientation="Horizontal" Margin="10">
      <Button x:Name="btnAdd" Content="Zpis" Click="btnAdd_Click" />
      <Button x:Name="btnRemove" Content="Vyma ve" Click="btnRemove_Click" />
    </StackPanel>
    <TextBox x:Name="tbOznam" Width="380"/>
    <ListBox x:Name="lbIS" Grid.Row="0" >
      <ListBox.ItemTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding nazev}" Margin="5,0,0,0"></TextBlock>
            <TextBlock Text="{Binding hodnota}" Margin="5,0,0,0"></TextBlock>
          </StackPanel>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </StackPanel>
</Grid>





strana 270
----------
C#:
public class Udaje
{
  public string nazev { get; set; }
  public string hodnota { get; set; }
}




private IsolatedStorageSettings isData = 
  IsolatedStorageSettings.ApplicationSettings;
ObservableCollection<Udaje> ocUdaje = 
  new ObservableCollection<Udaje>();

public MainPage()
{
  InitializeComponent();

  foreach (string key in isData.Keys)
  {
    string sHodnota;
    if (IsolatedStorageSettings.ApplicationSettings.TryGetValue
      (key,  out sHodnota))
    {
      ocUdaje.Add(new Udaje() { nazev = key, hodnota = sHodnota});
    }
  }
  lbIS.ItemsSource = ocUdaje;
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
  try
  {
    isData.Add(tbNazev.Text, tbHodnota.Text);
  }
  catch (ArgumentException ex)
  {
    tbOznam.Text = ex.Message;
  }
  Udaje u = new Udaje() { nazev = tbNazev.Text, hodnota = tbHodnota.Text };
  ocUdaje.Add(u);
}

private void btnRemove_Click(object sender, RoutedEventArgs e)
{
  ocUdaje.Clear();
  isData.Clear();
}




strana 271
----------
C#:
public class Knihy
{
  public string autor { get; set; }
  public string nazev { get; set; }
  public int cena { get; set; }
}


XAML:
<Grid x:Name="LayoutRoot" Background="White">
  <Grid.RowDefinitions>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
  </Grid.RowDefinitions>

  <ListBox x:Name="lbKnihy" Grid.Row="0">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="{Binding autor}" Margin="5,0,0,0"></TextBlock>
          <TextBlock Text="{Binding nazev}" Margin="5,0,0,0"></TextBlock>
          <TextBlock Text="{Binding cena}" Margin="5,0,0,0"></TextBlock>
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>

  <Button x:Name="btUloz" Grid.Row="1" Content="Ulo" 
    Width="100" Margin="2" Click="btUloz_Click"></Button>
  <Button x:Name="btCti" Grid.Row="2" Content="Nati" 
    Width="100" Margin="2" Click="btCitaj_Click"></Button>
  <Button x:Name="btVymaz" Grid.Row="3" Content="Vyma" 
    Width="100" Margin="2" Click="btVymaz_Click"></Button>
</Grid>



strana 272
----------
C#:
lKnihovna = (new List<Knihy>()
{
  new Knihy(){autor="Rowlingov", nazev="Harry Potter 1", cena=249},
  new Knihy(){autor="Adams", nazev="Stopav prvodce", cena=199},
  new Knihy(){autor="Hecko", nazev="erven vno", cena=333}
});



strana 273
----------
C#:
List<Knihy> lKnihovna = new List<Knihy>();

public MainPage()
{
  InitializeComponent();
  this.Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)
{
  lKnihovna = (new List<Knihy>()
  {
    new Knihy(){autor="Rowlingov", nazev="Harry Potter 1", cena=249},
    new Knihy(){autor="Adams", nazev="Stopav prvodce", cena=199},
    new Knihy(){autor="Hecko", nazev="erven vno", cena=333}
  });
}

public static void UlozKnihy(List<Knihy> knihy)
{
  IsolatedStorageSettings.ApplicationSettings["MojeKnihy"] = knihy;
}

public static List<Knihy> DajKnihy()
{
  List<Knihy> seznam = null;
  if (IsolatedStorageSettings.ApplicationSettings.Contains("MojeKnihy"))
  {
    seznam = IsolatedStorageSettings.ApplicationSettings["MojeKnihy"] as
    List<Knihy>;
  }
  return (seznam);
}

public static void VymazKnihy()
{
  IsolatedStorageSettings.ApplicationSettings.Remove("MojeKnihy");
}

private void btUloz_Click(object sender, RoutedEventArgs e)
{
  UlozKnihy(lKnihovna);
}

private void btCti_Click(object sender, RoutedEventArgs e)
{
  lKnihovna = DejKnihy();
  lbKnihy.ItemsSource = lKnihovna;
}

private void btVymaz_Click(object sender, RoutedEventArgs e)
{
  lKnihovna.Clear();
}



strana 274
----------
<Grid x:Name="LayoutRoot" Background="White">
  <Grid.RowDefinitions>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
  </Grid.RowDefinitions>

  <TextBox x:Name="tbEdit" Grid.Row="0" AcceptsReturn="True" 
    Margin="2" BorderThickness="2" Background="LightGray" 
    FontFamily="Courier New" FontSize="12"></TextBox>

  <Button x:Name="btUloz" Grid.Row="1" Content="Ulo" 
    Width="100" Margin="2" Click="btUloz_Click"></Button>
  <Button x:Name="btCitaj" Grid.Row="2" Content="Nati" 
    Width="100" Margin="2" Click="btCitaj_Click"></Button>
</Grid>


C#:
using System.IO;
using System.IO.IsolatedStorage;


private void btUloz_Click(object sender, RoutedEventArgs e)
{
  Save(tbEdit.Text, "ISedit.txt");
}

private void btCti_Click(object sender, RoutedEventArgs e)
{
  tbEdit.Text = Load("ISedit.txt");
}

private void Save(string data, string sNazev) 
{
  using (IsolatedStorageFile isf = 
    IsolatedStorageFile.GetUserStoreForApplication()) 
  {
    using (IsolatedStorageFileStream isfs = 
      new IsolatedStorageFileStream(sNazev, FileMode.Create, isf)) 
    {
      using (StreamWriter sw = new StreamWriter(isfs)) 
      {
         sw.Write(data); sw.Close(); 
      }
    }
  }
} 

private string Load(string sNazev) 
{
  string sText = String.Empty; 
  using (IsolatedStorageFile isf = 
    IsolatedStorageFile.GetUserStoreForApplication()) 
  {
    using (IsolatedStorageFileStream isfs = 
      new IsolatedStorageFileStream(sNazev, FileMode.Open, isf)) 
    { 
      using (StreamReader sr = new StreamReader(isfs)) 
      { 
        string sRadek = String.Empty; 
        while ((sRadek = sr.ReadLine()) != null)
        sText+= sRadek; 
      } 
    } 
  }
  return sText; 
}


strana 276
----------
<Grid x:Name="LayoutRoot" Background="White">
  <Grid.RowDefinitions>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
  </Grid.RowDefinitions>

  <ListBox x:Name="lbKnihy" Grid.Row="0">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="{Binding autor}" Margin="5,0,0,0"></TextBlock>
          <TextBlock Text="{Binding nazev}" Margin="5,0,0,0"></TextBlock>
          <TextBlock Text="{Binding cena}" Margin="5,0,0,0"></TextBlock>
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>

  <Button x:Name="btUloz" Grid.Row="1" Content="Ulo" 
    Width="100" Margin="2" Click="btUloz_Click"></Button>
  <Button x:Name="btCti" Grid.Row="2" Content="Nati" 
    Width="100" Margin="2" Click="btCitaj_Click"></Button>
  <Button x:Name="btVymaz" Grid.Row="3" Content="Vyma" 
    Width="100" Margin="2" Click="btVymaz_Click"></Button>
</Grid>





strana 277
----------
C#:
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;


public class Knihy
{
  public string autor { get; set; }
  public string nazev { get; set; }
  public int cena { get; set; }
}



strana 278
----------
C#:
ObservableCollection<Knihy> ocKnihovna = new ObservableCollection<Knihy>();

public MainPage()
{
  InitializeComponent();
  ocKnihovna = (new ObservableCollection <Knihy>()
  {
    new Knihy(){autor="Rowlingov", nazev="Harry Potter 1", cena=249},
    new Knihy(){autor="Adams", nazev="Stopav prvodce", cena=199},
    new Knihy(){autor="Hecko", nazev="erven vno", cena=333}
  });
  lbKnihy.ItemsSource = ocKniznica;

}

private void btUloz_Click(object sender, RoutedEventArgs e)
{
  UlozKnihy(ocKnihovna);
}

private void btCitaj_Click(object sender, RoutedEventArgs e)
{
  List<Knihy> pom = DejKnihy();

  foreach (Knihy k in pom)
  {
    ocKnihovna.Add(k);
  }
}

private void btVymaz_Click(object sender, RoutedEventArgs e)
{
  IsolatedStorageFile.GetUserStoreForApplication().DeleteFile("knihy.xml");
}

public static void UlozKnihy(ObservableCollection<Knihy> knihy)
{
  IsolatedStorageFile soubor =
    IsolatedStorageFile.GetUserStoreForApplication();
  using (IsolatedStorageFileStream stream = soubor.CreateFile("knihy.xml"))
  {
    XElement data = new XElement("knihy",
    from k in knihy
    select new XElement("kniha",
    new XAttribute("Autor", k.autor),
    new XAttribute("Nzev", k.nazev),
    new XAttribute("Cena", k.cena)));

    using (XmlWriter writer = XmlWriter.Create(stream))
    {
      data.Save(writer);
      writer.Close();
    }
    stream.Close();
  }
}

public static List<Knihy> DejKnihy()
{
  List<Knihy> seznam = null;
  try
  {

    IsolatedStorageFile soubor = 
      IsolatedStorageFile.GetUserStoreForApplication();
    using (IsolatedStorageFileStream stream = 
      soubor.OpenFile("knihy.xml", FileMode.Open))
    {
      using (XmlReader reader = XmlReader.Create(stream))
      {
        XElement data = XElement.Load(reader);
        seznam = (from k in data.Elements("kniha")
          select new Knihy()
        {
          autor = (string)k.Attribute("Autor"),
          nazev = (string)k.Attribute("Nzev"),
          cena = (int)k.Attribute("Cena")
        }).ToList();
      }
      stream.Close();
    }
  }
  catch{}
  return (seznam);
}



